/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import gui.AexBanner;
import interfaces.IEffectenbeurs;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

/**
 *
 * @author willem, aangepast door elton
 */
public class BannerController {
    static AexBanner myAEXBanner;
    IEffectenbeurs myEffectenbeurs;

    // Set binding name for student administration
    private static String bindingName = "MockEffectenbeurs";
    
    // References to registry and student administration
    private Registry      registry = null;
    
    public BannerController(String ipAddress, int portNumber)
    {
        
        // Print IP address and port number for registry
        System.out.println("Client: IP Address: " + ipAddress);
        System.out.println("Client: Port number " + portNumber);
        
        // Bind student administration
        
            // Locate registry at IP address and port number
            registry = locateRegistry(ipAddress,portNumber);
        
            // Print result locating registry
            if (registry != null) {
                System.out.println("Client: Registry located");
            }
            else {
                System.out.println("Client: Cannot locate registry");
                System.out.println("Client: Registry is null pointer");
            }

        
        
            // Bind student administration using registry
            if (registry != null) {
                myEffectenbeurs = bindEffectenbeursUsingRegistry();
            }
      
        
        // Print result binding student administration
        if (myEffectenbeurs != null) {
            System.out.println("Client: Efectenbeurs bound");
            myAEXBanner= new AexBanner();
            myAEXBanner.setVisible(true); 
        }
        else {
            System.out.println("Client: Effectenbeurs is null pointer");
        }
    }
    
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Client: Enter IP address of server: ");
        String ipAddress = input.nextLine();
        
        // Get port number
        System.out.print("Client: Enter port number: ");
        int portNumber = input.nextInt();
        
        BannerController controller = new BannerController(ipAddress, portNumber);
    }
    
     // Locate registry
    private Registry locateRegistry(String ipAddress, int portNumber) {
        
        // Locate registry at IP address and port number
        Registry registry = null;
        try {
            registry = LocateRegistry.getRegistry(ipAddress,portNumber);
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot locate registry");
            System.out.println("Client: RemoteException: " + ex.getMessage());
            registry = null;
        }
        return registry;
    }
    
     // Bind student administration using registry
    private IEffectenbeurs bindEffectenbeursUsingRegistry() {
                
        // Bind student administration
        IEffectenbeurs beurs  = null;
        try {
            Object o = registry.lookup(bindingName);
            System.out.println(o.getClass().toString());
            beurs = (IEffectenbeurs) o;
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: RemoteException: " + ex.getMessage());
            beurs = null;
        } catch (NotBoundException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: NotBoundException: " + ex.getMessage());
            beurs = null;
        }
        return beurs;
    }
    
    public BannerController (IEffectenbeurs eb, AexBanner banner) {
        myEffectenbeurs = eb;
        myAEXBanner = banner;
    }
   
    
}
